package com.weixin.paleituo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.TemplateDetailsBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.RoundCircleTransform;

import java.util.List;

import butterknife.BindView;

/**
 * 共享家，基本信息
 *
 * @author fengzhen
 * @version v1.0, 2018/4/20
 */
public class TemplateInfoFragment extends BaseFragment {

    @BindView(R.id.tv_building_fragment_template_info)
    TextView mTvBuildingFragmentTemplateInfo;
    @BindView(R.id.tv_location_fragment_template_info)
    TextView mTvLocationFragmentTemplateInfo;
    @BindView(R.id.img_fragment_template_info)
    ImageView mImgFragmentTemplateInfo;
    @BindView(R.id.tv_name_fragment_template_info)
    TextView mTvNameFragmentTemplateInfo;
    @BindView(R.id.tv_door_fragment_template_info)
    TextView mTvDoorFragmentTemplateInfo;
    @BindView(R.id.tv_num_fragment_template_info)
    TextView mTvNumFragmentTemplateInfo;
    @BindView(R.id.tv_info_fragment_template_info)
    TextView mTvInfoFragmentTemplateInfo;
    @BindView(R.id.linear_tags_fragment_template_info)
    LinearLayout mLinearTags;

    private TemplateDetailsBean mTemplate;

    public static TemplateInfoFragment newInstance(TemplateDetailsBean template) {
        Bundle args = new Bundle();
        TemplateInfoFragment fragment = new TemplateInfoFragment();
        args.putSerializable(Contans.INTENT_DATA, template);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View initView(Context context) {
        return UIUtils.inflate(getContext(), R.layout.fragment_template_info);
    }

    @Override
    protected void initData() {
        super.initData();

        mTemplate = (TemplateDetailsBean) getArguments().getSerializable(Contans.INTENT_DATA);

        initUI();
    }

    private void initUI() {
        mTvBuildingFragmentTemplateInfo.setText(mTemplate.getHouses());
        mTvLocationFragmentTemplateInfo.setText(mTemplate.getAddress());
        Glide.with(getContext())
                .load(mTemplate.getSurfaceUrl())
                .asBitmap()
                .transform(new RoundCircleTransform(mContext, 6))
                .error(R.mipmap.buffer)
                .placeholder(R.mipmap.buffer)
                .into(mImgFragmentTemplateInfo);
        mTvNameFragmentTemplateInfo.setText(mTemplate.getName());
        mTvDoorFragmentTemplateInfo.setText(new StringBuffer(mTemplate.getHouses()).append(mTemplate.getFullname()));
        initTags(mTemplate.getTag());
        mTvNumFragmentTemplateInfo.setText(String.format(getString(R.string.string_person_num), mTemplate.getNop()));
        mTvInfoFragmentTemplateInfo.setText(mTemplate.getBriefDescription());
    }

    /**
     * 初始化房屋标签
     */
    private void initTags(List<String> tags) {
        if (tags == null) return;
        int tagPosition = -1;
        for (String tag : tags) {
            if (!StringUtils.isEmpty(tag)) {
                tagPosition++;
                if (tagPosition <= 2) {
                    TextView textView = new TextView(mContext);
                    if (tagPosition == 0) {
                        textView.setTextColor(UIUtils.getColor(R.color.c_ff37f4c5));
                        textView.setBackgroundResource(R.drawable.tag_1_shape);
                    } else if (tagPosition == 2) {
                        textView.setTextColor(UIUtils.getColor(R.color.c_FDA444));
                        textView.setBackgroundResource(R.drawable.tag_4_shape);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        layoutParams.setMargins(UIUtils.dip2px(20), 0, 0, 0);
                        textView.setLayoutParams(layoutParams);
                    } else if (tagPosition == 1) {
                        textView.setTextColor(UIUtils.getColor(R.color.c_d84cfb));
                        textView.setBackgroundResource(R.drawable.tag_3_shape);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        layoutParams.setMargins(UIUtils.dip2px(20), 0, 0, 0);
                        textView.setLayoutParams(layoutParams);
                    }
                    textView.setPadding(UIUtils.dip2px(5), UIUtils.dip2px(2), UIUtils.dip2px(5), UIUtils.dip2px(2));
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, UIUtils.px2dip(UIUtils.getDimens(R.dimen.sp_11)));
                    textView.setGravity(Gravity.CENTER);
                    textView.setText(tag);
                    mLinearTags.addView(textView);
                }
            }
        }
    }
}
